package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.common.result.Result;
import com.system.runtai.entity.EmergencyVehicle;
import com.system.runtai.entity.Feedback;
import com.system.runtai.service.FeedbackService;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 反馈信息 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "反馈信息")
@RestController
@RequestMapping("/runtai/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @ApiOperation(value = "查看所有反馈信息")
    @GetMapping("/feedbackList")
    public Result<IPage<Feedback>> feedbackList(@RequestParam(required = false) String intro,@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit){
//        List<Feedback> list = feedbackService.list();


        if (StringUtils.isEmpty(intro)){
            Page<Feedback> recommendPage = new Page<>(page,limit);
            IPage<Feedback> iPage = feedbackService.page(recommendPage);
            return Result.ok(iPage);
        }else if (!StringUtils.isEmpty(intro)){
            LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(Feedback::getIntro,intro);

            Page<Feedback> recommendPage = new Page<>(page,limit);
            IPage<Feedback> iPage = feedbackService.page(recommendPage,wrapper);
            return Result.ok(iPage);
        }
        Page<Feedback> recommendPage = new Page<>(page,limit);
        IPage<Feedback> iPage = null;
        return Result.fail(iPage).message("查询失败！数据为空！");
    }

    @ApiOperation(value = "查看自己发布反馈信息")
    @GetMapping("/myfeedbackList")
    public Result<IPage<Feedback>> myfeedbackList(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit){
        String s = AuthContextUtil.get();
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Feedback::getAppletUserId,Integer.valueOf(s)).orderByDesc(Feedback::getCreateTime);
        List<Feedback> list = feedbackService.list(wrapper);

        Page<Feedback> recommendPage = new Page<>(page,limit);
        IPage<Feedback> iPage = feedbackService.page(recommendPage,wrapper);
        return Result.ok(iPage);
    }

    @ApiOperation(value = "查看所有反馈信息")
    @GetMapping("/myfeedbackById/{id}")
    public Result<Feedback> myfeedbackById(@PathVariable Integer id){
        Feedback byId = feedbackService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "添加反馈信息")
    @PostMapping("/saveFeedback")
    public Result<Feedback> saveFeedback(@RequestBody Feedback feedback){

        String s = AuthContextUtil.get();
        feedback.setAppletUserId(Integer.valueOf(s));
        feedback.setCreateTime(LocalDateTime.now());
        boolean save = feedbackService.save(feedback);
if (save){
    return Result.ok(feedback).message("添加反馈成功");
}
        return Result.fail(feedback).message("添加反馈失败");
    }

    @ApiOperation(value = "删除反馈信息")
    @PostMapping("/delectFeedback/{id}")
    public Result delectFeedback(@PathVariable Integer id){

        boolean byId = feedbackService.removeById(id);
        if (byId){
            return Result.ok().message("删除反馈成功");
        }
        return Result.fail().message("删除反馈失败");
    }



}

